package jmc.util;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.persistence.Transient;



public class DateUtil {
	
	@Transient
	public static boolean getDaysCompareEntryToNow(Timestamp fecEntrada, long days){
		boolean out = false;
		Date date = new Date();
		Timestamp fechaActual = new Timestamp(date.getTime());
		//84600000 milliseconds in a day
		long oneDay = (1 * 24 * 60 * 60 * 1000);
		long nDay = oneDay * days; 
		//to add to the timestamp
		
		
		//System.out.println("fechaEntrada " + fecEntrada );
		//System.out.println("fechaActual " + fechaActual );
		
		long tsEntrada = fecEntrada.getTime();
		long tsActual = fechaActual.getTime();
		
		long tsResult =   tsActual - tsEntrada;
									
		long day = tsResult / oneDay;
		
		if (day > days){
			out = true;
		} 
		return out;		
	}
	
	public static Timestamp restaTimestampByDays(Timestamp fecha, Long days) {
		Long oneDay = (1l * 24l * 60l * 60l * 1000l);
		days = oneDay * days;
		Long tsResult = fecha.getTime() - days;
		fecha.setTime(tsResult);		
		return fecha;
	}
	
	public static Timestamp sumaTimestampByDays(Timestamp fecha, Long days) {
		Long oneDay = (1l * 24l * 60l * 60l * 1000l);
		days = oneDay * days;
		Long tsResult = fecha.getTime() + days;
		fecha.setTime(tsResult);		
		return fecha;
	}
	public static String difTimeStampToTimeStamp(Timestamp fechaInicial, Timestamp fechaFinal) {
		String result = "";

		long oneDay = (24 * 60 * 60 * 1000);
		long oneHour = (60 * 60 * 1000);
		long oneMinute = (60 * 1000);
		if (fechaFinal==null){
			Date date = new Date();
			fechaFinal = new Timestamp(date.getTime());
		}
		Long tsEntrada = fechaInicial.getTime();
		Long tsActual = fechaFinal.getTime();
		
		long tsResult =  tsActual - tsEntrada;
		
		
		Timestamp tiempoDif = new Timestamp(tsResult);			
		
		System.out.println("Resultado " + tiempoDif);
		/*
		 * Obtengo los dias
		 */
		Long dias = tsResult / oneDay;
		/*
		 * Rescato el resto de la divicion de dias para obtener las horas.
		 */
		Long restoDias = tsResult % oneDay;
		/*
		 * Obtengo las horas en base del resto de los dias.
		 */						
		Long horas = restoDias / oneHour;
		/*
		 * Rescato el resto de la divicion de horas para obtener las minutos.
		 */
		Long restoHoras = restoDias % oneHour;
		/*
		 * Obtengo los minutos en base del resto de los Horas.
		 */	
		Long minutos = restoHoras / oneMinute;
		
		System.out.println("dias " + dias);
		System.out.println("horas " + horas);
		System.out.println("minutos " + minutos);

		result = dias + "d " + horas + "h " + minutos + "m";
		
		return result;
	}
	
	public static String getMonthName(int monthNumber) {
        String month = null;  
		switch (monthNumber) {
            case 0: month = " Enero ";break;
            case 1: month = " Febrero "; break;
            case 2: month = " Marzo "; break;
            case 3: month = " Abril "; break;
            case 4: month = " Mayo "; break;
            case 5: month = " Junio "; break;
            case 6: month = " Julio "; break;
            case 7: month = " Agosto "; break;
            case 8: month = " Septiembre "; break;
            case 9: month = " Octubre "; break;
            case 10: month = " Noviembre "; break;
            case 11: month = " Diciembre "; break;
        }
        return month;
	}
	
	public static String getFormattedDate(Date date){
		
		int day,month,year;
		Calendar cal = new GregorianCalendar();
		String result = "";
		if(date != null){
			cal.setTime(date);
			
			day = cal.get(Calendar.DATE);
			month = cal.get(Calendar.MONTH);
			year = cal.get(Calendar.YEAR);
			
			result = String.valueOf(day) + " de" + getMonthName(month) + "de " + String.valueOf(year);
		}
		return result;
	}
	public static Timestamp composeDate(String date){
		Calendar cal=new GregorianCalendar();
		int year = Integer.parseInt(date.substring(date.lastIndexOf("/")+1,date.length()));
		int month = Integer.parseInt(date.substring(date.indexOf("/")+1,date.lastIndexOf("/")))-1;
		int day = Integer.parseInt(date.substring(0, date.indexOf("/")));
		cal.set(year, month, day);
		return new Timestamp(cal.getTime().getTime());
	} 

	public static Timestamp composeDateAS(String date){

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		java.util.Date parsedDate = null;
		try {
			parsedDate = dateFormat.parse(date);
		} catch (ParseException e) {

			e.printStackTrace();
		}
		java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());

		return timestamp;
	} 

	public static Timestamp composeDateTimeAS(String date){

		if (date == null || date.trim().equalsIgnoreCase("") ){
			return null;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
		java.util.Date parsedDate = null;
		try {
			parsedDate = dateFormat.parse(date);
		} catch (ParseException e) {

			e.printStackTrace();
		}
		java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());

		return timestamp;
	} 

	public static String getFormatedDate (Timestamp date){
		if (date != null){
			SimpleDateFormat format = new SimpleDateFormat("dd'/'MM'/'yyyy' 'HH:mm");
			return (date==null)?" - ":format.format(new Date(date.getTime()));
		}else{
			return "";
		}

	}
	
	public static String getFormatedCanonicalDate (Timestamp date){
		if (date != null){
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			return (date==null)?" - ":format.format(new Date(date.getTime()));
		}else{
			return null;
		}

	}

	public static String getFormatedCanonicalDateTime (Timestamp date){
		if (date != null){
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
			return (date==null)?" - ":format.format(new Date(date.getTime()));
		}else{
			return null;
		}

	}

	public static String getFormatedCanonicalDateTimeV1 (Timestamp date){
		if (date != null){
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
			return (date==null)?" - ":format.format(new Date(date.getTime()));
		}else{
			return null;
		}
	}
	
	public static Integer getFormatedCanonicalTimeV_2 (Timestamp date){
		Integer result = 0;
		if (date != null){
			SimpleDateFormat format = new SimpleDateFormat("HHmm");
			String resultStr = format.format(new Date(date.getTime()));
			try{
				result = Integer.parseInt(resultStr);
			}catch(NumberFormatException ne){
				return 0;
			}
			return result;
		}else{
			return result;
		}
	}

	public static Integer getFormatedCanonicalDateV_2 (Timestamp date){
		Integer result = 0;
		if (date != null){
			SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
			String resultStr = format.format(new Date(date.getTime()));
			resultStr = "1" + resultStr;
			try{
				result = Integer.parseInt(resultStr);
			}catch(NumberFormatException ne){
				return 0;
			}
			return result;
		}else{
			return result;
		}
	}

	public static String getFormatedDataAs400(String date){

		String result = "";
		date = date.trim();
		if (date.equals("") || date == null || date.length() < 5){
			
		}else{
			
			if (date.length() < 6){
				date = "0" + date;
			}
			result = date.substring(0,2) + "/" + date.substring(2, 4) + "/" + date.substring(4, 6);
		}
		return result;	
	}
	public static String getFormatedDataAs400New(String date){
		String result = "";
		date = date.trim();
		if (date != null && date.trim().equals("0")){
			return "";
		}

		if (date.equals("") || date == null || date.length() < 6){			
		}else{			
			if (date.length() < 7){
				result = date.substring(4, 6)+ "/" + date.substring(2, 4) + "/" + date.substring(0,2) ;
			}else{
				result = date.substring(5, 7)+ "/" + date.substring(3, 5) + "/" + date.substring(1,3) ;
			}
			
		}
		return result;	
	}
	
	public static String getFormatedDataAs400New1(String date){
		String result = "";
		date = date.trim();
		if (date.equals("") || date == null || date.length() < 8){			
		}else{			
			if (date.length() < 9){
				result = date.substring(6, 8)+ "/" + date.substring(4, 6) + "/" + date.substring(2,4) ;
			}
			
		}
		return result;	
	}

	public static String getFormatedTimeAs400New(String time){
		String result = "";		
		if (time != null && time.trim().equals("0")){
			return "";
		}
		time = time.trim();
		if(time.length() == 5){
			time = "0" + time;
		}
		if(time.length() == 3){
			time = "0" + time;
		}
		if (time.length() == 6){
			result = time.substring(0, 2)+ ":" + time.substring(2, 4) + ":" + time.substring(4,6) ;
		}
		if (time.length() == 4){
			result = time.substring(0, 2)+ ":" + time.substring(2, 4);
		}
		return result;	
	}

	public static String castFecha(String fechaProceso) {
		String result = "";
		try{
			result = fechaProceso.substring(4, 8) + fechaProceso.substring(2, 4) + fechaProceso.substring(0, 2) ;   
		}catch(Exception e ){
			e.printStackTrace();
		}
		
		return result;
	}

}
